 /**
 * 逻辑、功能相关描述:
 *
 * @author 王浩羽 编写日期：Dec 29, 2008
 */
package com.ce.pms.system.occupation.service.impl;

import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.service.impl.AbstractCustomerEventServiceImpl;
import com.ce.pms.system.occupation.dao.IPerfectItemTypeConfigDao;
import com.ce.pms.system.occupation.model.PerfectItemTypeConfigDO;
import com.ce.pms.system.occupation.service.IPerfectItemTypeConfigService;
import com.ce.pms.system.occupation.vo.PerfectItemTypeConfigVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 整改项目类型设置DAO
 * @author 王浩羽
 *
 */
public class PerfectItemTypeConfigServiceImpl extends AbstractCustomerEventServiceImpl<PerfectItemTypeConfigVO, PerfectItemTypeConfigDO> implements
	IPerfectItemTypeConfigService
{
	private IPerfectItemTypeConfigDao perfectItemTypeConfigDao;
	
	public boolean addPerfectItemTypeConfig(PerfectItemTypeConfigVO perfectItemTypeConfigVO)
	{
		if(!BlankUtil.isBlank(perfectItemTypeConfigVO) && !BlankUtil.isBlank(perfectItemTypeConfigVO.getPerfectItemTypeName()))
		{
			if(isNameExisit(perfectItemTypeConfigVO.getPerfectItemTypeName())){
				throw new BusinessException(getNameExisitExceptionInoKey(),BusinessException.WARNING);
			}
			PerfectItemTypeConfigDO perfectItemTypeConfigDO = assembleV2D(perfectItemTypeConfigVO,null);
			perfectItemTypeConfigDO.setSysInfo(createInfo(perfectItemTypeConfigVO.getCreator()));
			return perfectItemTypeConfigDao.saveObject(perfectItemTypeConfigDO) != null;
		}
		return false;
	}
	
	
	public boolean deletePerfectItemTypeConfigById(Long perfectItemTypeConfigId)
	{
		if(perfectItemTypeConfigId==null)
		{
			return false;
		}
		try
		{
			perfectItemTypeConfigDao.deleteObjectById(perfectItemTypeConfigId);
			return true;
		}
		catch(RuntimeException e)
		{	//如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("occupation:system_delete_inused_exception",BusinessException.WARNING);
		}
	}
	
	public boolean editPerfectItemTypeConfig(PerfectItemTypeConfigVO perfectItemTypeConfigVO)
	{
		if(!BlankUtil.isBlank(perfectItemTypeConfigVO) && !BlankUtil.isBlank(perfectItemTypeConfigVO.getKeyword()))
		{
			PerfectItemTypeConfigDO perfectItemTypeConfigDO = perfectItemTypeConfigDao.findById(perfectItemTypeConfigVO.getKeyword());
			if(perfectItemTypeConfigDO==null)
			{
				return false;
			}
			perfectItemTypeConfigDO.setPerfectItemTypeName(perfectItemTypeConfigVO.getPerfectItemTypeName());
			perfectItemTypeConfigDO.setDescription(perfectItemTypeConfigVO.getDescription());
			perfectItemTypeConfigDO.setSequence(perfectItemTypeConfigVO.getSequence());
			modifyInfo(perfectItemTypeConfigDO.getSysInfo(), perfectItemTypeConfigVO.getModifier());
			return perfectItemTypeConfigDao.updateObject(perfectItemTypeConfigDO) != null;
		}
		return false;
	}
	
	protected boolean isNameExisit(String contractorInfoName){
		return perfectItemTypeConfigDao.findUniqueByName(contractorInfoName)!=null;
	}
	
	public Page queryPerfectItemTypeConfigListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}
	
	@Override
	protected int compareDO(PerfectItemTypeConfigDO o1, PerfectItemTypeConfigDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}
	
	public void setPerfectItemTypeConfigDao(IPerfectItemTypeConfigDao perfectItemTypeConfigDao)
	{
		this.perfectItemTypeConfigDao = perfectItemTypeConfigDao;
	}
	

	public List<PerfectItemTypeConfigVO> queryAllPerfectItemTypeConfig()
	{
		List<PerfectItemTypeConfigDO> perfectItemTypeConfigDOList = perfectItemTypeConfigDao.findAll();
		sortList(perfectItemTypeConfigDOList);
		return assembleDOList2VOList(perfectItemTypeConfigDOList);
	}


	@Override
	protected PerfectItemTypeConfigDO findObjectDOById(Long id)
	{
		return perfectItemTypeConfigDao.findById(id);
	}


	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		Page page = perfectItemTypeConfigDao.queryByPage(searchVO);
		List<PerfectItemTypeConfigDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}
	
	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "occupation:system_name_exist_exception";
	}
}
